home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #26 (Nov 87) / asm lab Formatter / FormatDemo.asm < prev    next >
Assembly Source File  |  1987-10-22  |  6KB  |  280 lines

  1. ; FormatDemo.asm
  2. ;----------------
  3. ; Written by Mike™ Scanlin 1 JAN 1987
  4. ; Slightly modified by D. Smith
  5. ; for MacTutor. 19 OCT 1987
  6. ; driver program to demonstrate DrawNumsInAString
  7.  
  8. Include    Traps.D
  9. Include ToolEqu.D
  10.  
  11. Xref    DrawNumsInAString
  12.  
  13. ;Quickdraw Equates Used:
  14. srcCopy         EQU  0        ;copybits mode
  15. portBits        EQU  $2      ;port's bitmap [bitmap]
  16. portRect        EQU  $10    ;port's rectangle
  17.  
  18. FormatDemo:
  19.     PEA        -4(A5)    ;init everything
  20.     _InitGraf        ;graf port
  21.     _InitFonts        ;fonts
  22.     _InitWindows    ;windows
  23.     _InitMenus        ;menus
  24.     PEA        Quit    ;resume routine
  25.     _InitDialogs    ;dialogs
  26.     _TEInit            ;text edit
  27.     MOVE.L    #$FFFF,D0
  28.     _FlushEvents    ;clear event queque
  29.     _InitCursor        ;cursor
  30.     
  31.     CLR.L    -(SP)        ;returned menu handle
  32.     MOVE.W    #1,-(SP)    ;menu ID 1
  33.     _GetRMenu            ;get resource menu
  34.     MOVE.L    (SP), AppleHandle(A5)    ;save menu handle
  35.     MOVE.L    (SP), -(SP)    ;put back on stack
  36.     CLR.W    -(SP)        ;add to menu end
  37.     _InsertMenu            ;apple menu added
  38.     MOVE.L    #'DRVR', -(SP)    ;get das
  39.     _AddResMenu            ;add das to menu
  40.     CLR.L -(SP)            ;ret menu handle
  41.     MOVE.W    #2, -(SP)    ;menu ID 2
  42.     _GetRMenu            ;get resource menu
  43.     CLR.W -(SP)            ;add to end
  44.     _InsertMenu            ;file menu
  45.     CLR.L -(SP)            ;ret menu handle
  46.     MOVE.W    #3, -(SP)    ;menu ID 3
  47.     _GetRMenu            ;get resource menu
  48.     CLR.W -(SP)            ;add to end
  49.     _InsertMenu            ;message menu
  50.     _DrawMenuBar        ;do menu bar
  51.     
  52.     CLR.L    -(SP)        ;return window ptr
  53.     CLR.L    -(SP)        ;heap window record
  54.     PEA        wRect        ;default rect size
  55.     PEA        wTitle        ;default tile
  56.     MOVE    #$100,-(SP)    ;true for visible
  57.     MOVE    #noGrowDocProc,-(SP)    ;window type
  58.     MOVE.L    #-1,-(SP)    ;window in front
  59.     CLR        -(SP)        ;no close box
  60.     MOVE.L    #3,-(SP)    ;reference value
  61.     _NewWindow            ;make heap window
  62.     
  63.     MOVE.L    (SP),wPointer(A5)    ;save window ptr
  64.     _SetPort            ;set graf port to window
  65.     _PenNormal            ;init pen
  66.     MOVE    #4,-(SP)    ;any mono-spaced font will do
  67.     _TextFont            ;font
  68.     MOVE    #0,-(SP)    ;normal
  69.     _TextFace            ;style
  70.     MOVE    #12,-(SP)    ;12 point type
  71.     _TextSize            ;size
  72.     MOVE.L    wPointer(A5), A0
  73.     PEA        portRect(A0)
  74.     _ClipRect            ;set clip region
  75.     
  76. GetEvent:
  77.     _SystemTask            ;check das
  78.     CLR        -(SP)        ;returned event
  79.     MOVE    #-1,-(SP)    ;mask all events
  80.     PEA        EventRecord(A5)    ;event record
  81.     _GetNextEvent        ;get next event
  82.     MOVE    (SP)+,D0    ;pop result
  83.     BEQ.S    GetEvent    ;0=null event
  84.     MOVE    What(A5),D0    ;what event?
  85.     CMPI    #1,D0        ;mouseDown event?
  86.     BNE.S    GetEvent    ;no, try again
  87.     
  88. ;handle a mouse down event
  89.     CLR        -(SP)        ;yes, where mouse?
  90.     MOVE.L    Where(A5),-(SP)
  91.     PEA        fndWindow(A5)
  92.     _FindWindow            ;find window
  93.     MOVE    (SP)+,D0    ;pop result
  94.     CMPI    #1,D0        ;in menu bar?
  95.     BEQ        MenuBar        ;check quit 
  96.     CMPI    #2,D0        ;in system window?
  97.     BEQ        System        ;do sysem stuff
  98.     CMPI    #4,D0        ;drag region?
  99.     BEQ        DragIt        ;do drag
  100.     CMPI    #3,D0        ;content region?
  101.     BNE.S    GetEvent    ;no, ignore it
  102.     
  103. ;do content click
  104.     CLR.L    -(SP)        ;yes, do click
  105.     _FrontWindow        ;window up?
  106.     MOVE.L    (SP)+, D0    ;pop result
  107.     CMP.L    wPointer(A5), D0    ;our window up?
  108.     BEQ        DoIt        ;yes, do click
  109.     MOVE.L    wPointer(A5), -(SP)    ;no, activate
  110.     _SelectWindow    ;make it front
  111.     MOVE.L    wPointer(A5), -(SP)
  112.     _SetPort        ;point grafport to it
  113.     BRA    GetEvent    ;get next click
  114.     
  115. DoIt:    
  116. ;draw 10 strings in the window    
  117.     MOVE.L    wPointer(A5), A0
  118.     PEA    portRect(A0)    ;get wind port rect
  119.     _EraseRect            ;erase port rect
  120.     MOVE    #30,vPos(A5)    ;set pen coord
  121. ;do 5 addition problems
  122.     MOVE    #5,count(A5)    ;5 problems count    
  123. @1    MOVE    #56,-(SP)    ;set pen coord
  124.     MOVE    vPos(A5),-(SP)
  125.     _MoveTo                ;move pen
  126.     CLR        -(SP)        ;get 1st operand
  127.     _Random                ;a random number
  128.     MOVE    (SP)+,D3    ;pop result
  129.     EXT.L    D3
  130.     CLR        -(SP)        ;get 2nd operand
  131.     _Random                ;a random number
  132.     MOVE    (SP)+,D4    ;pop result
  133.     EXT.L    D4
  134.     MOVE.L    D3,D0
  135.     ADD.L    D4,D0        ;addition problem
  136.     MOVE.L    D0,-(SP)    ;push result
  137.     MOVE.L    D4,-(SP)    ;push 2nd param
  138.     MOVE.L    D3,-(SP)    ;push 1st param
  139.     PEA        FormatString2
  140.     JSR        DrawNumsInAString    ;format numbers
  141.     ADD        #20,vPos(A5)    ;reposition pen
  142.     SUB        #1,count(A5)    ;dec count
  143.     BNE        @1                ;do next problem
  144. ;do 5 division problems
  145.     MOVE    #5,count(A5)    ;count
  146. @3    MOVE    #35,-(SP)        ;pen position
  147.     MOVE    vPos(A5),-(SP)    ;pen position
  148.     _MoveTo                    ;set pen
  149.     CLR        -(SP)        ;get 20 bit random #
  150.     _Random
  151.     CLR        -(SP)
  152.     _Random
  153.     MOVE.L    (SP)+,D3
  154.     ANDI.L    #$FFFFF,D3
  155. @2    CLR        -(SP)        ;get 16 bit random #
  156.     _Random
  157.     MOVE    (SP)+,D1
  158.     BEQ.S    @2        ;don't divide by zero
  159.     MOVE.L    D3,D0
  160.     DIVS    D1,D0    ;do division problem
  161.     MOVE.L    D0,-(SP)    ;push fixed point result
  162.     MOVE    D1,-(SP)
  163.     EXT.L    D1
  164.     MOVE.L    D1,-(SP)    ;push 2nd longint
  165.     MOVE.L    D3,-(SP)    ;push 1st longint
  166.     PEA        FormatString1
  167.     JSR        DrawNumsInAString
  168.     ADD        #20,vPos(A5)
  169.     SUB        #1,count(A5)
  170.     BNE        @3
  171.     JSR        DoPic    ;create quickdraw pic
  172.     BRA        GetEvent
  173.     
  174. DragIt:
  175.     MOVE.L wPointer(A5), -(SP)    ;window ptr
  176.     MOVE.L    where(A5), -(SP)    ;mouse
  177.     PEA        dragbounds            ;bounds
  178.     _DragWindow                    ;drag it
  179.     BRA        GetEvent
  180.     
  181. System:
  182.     PEA        EventRecord(A5)
  183.     MOVE.L    fndWindow(A5), -(SP)
  184.     _SystemClick        ;support das
  185.     BRA        GetEvent
  186.     
  187. MenuBar:
  188.     CLR.L    -(SP)    ;menu choice
  189.     MOVE.L    Where(A5), -(SP)
  190.     _MenuSelect
  191.     MOVE.W    (SP)+,D5    ;menu
  192.     MOVE.W    (SP)+,D6    ;item
  193.     CLR.W    -(SP)
  194.     _HiLiteMenu
  195.     
  196.     CMP    #1,D5
  197.     BEQ    AppleMenu
  198.     CMP    #2,D5
  199.     BEQ    FileMenu
  200.     BRA    GetEvent
  201.     
  202. AppleMenu:
  203.     CMP    #1,D6
  204.     BEQ    About
  205.     
  206.     PEA    grafporttemp(A5)    ;do das
  207.     _GetPort
  208.     Move.L    AppleHandle(A5), -(SP)
  209.     Move.W    D6,-(SP)
  210.     PEA    DeskName(A5)
  211.     _GetItem        ;get da name
  212.     CLR    -(SP)
  213.     PEA    DeskName(A5)
  214.     _OpenDeskAcc    ;start up da
  215.     Move    (SP)+, D0    ;result
  216.     MOVE.L    grafporttemp(A5), -(SP)
  217.     _SetPort
  218.     BRA    GetEvent
  219.     
  220. About:
  221.     Move.W    #5, -(SP)
  222.     _SysBeep    ;sorry charlie!
  223.     BRA    GetEvent
  224.     
  225. FileMenu:
  226. Quit:
  227.     _ExitToShell
  228.  
  229. DoPic:
  230.     CLR.L    -(SP)    ;save pic of screen
  231.     MOVE.L    wPointer(A5), -(SP)
  232.     _GetWindowPic    ;get current pic
  233.     MOVE.L    (SP)+, A0
  234.     MOVE.L    A0, -(SP)
  235.     _KillPicture    ;kill it
  236.     CLR.L    -(SP)
  237.     MOVE.L    wPointer(A5), A0
  238.     PEA    portRect(A0)
  239.     _OpenPicture    ;open new pic
  240.     MOVE.L    (SP)+, A0
  241.     MOVE.L wPointer(A5), -(SP)
  242.     MOVE.L    A0, -(SP)
  243.     _SetWindowPic    ;set in window rec
  244.     MOVE.L    wPointer(A5), A0
  245.     PEA    portBits(A0)    ;set up copybits
  246.     PEA    portBits(A0)
  247.     PEA    portRect(A0)
  248.     PEA    portRect(A0)
  249.     MOVE.W    #srcCopy, -(SP)
  250.     CLR.L    -(SP)
  251.     _CopyBits    ;copy screen to pic
  252.     _ClosePicture    ;close pic
  253.     rts
  254.     
  255. ; Data structures - Constants
  256.  
  257. wRect            DC        60,80,300,420
  258. dragbounds        DC        20,40,330,492
  259. wTitle            DC.B    17,'Formatted Numbers'
  260. ;note: format strings must end with a zero byte
  261. FormatString1    DC.B    '\i,10 / \i,7 = \f,7.5',0
  262. FormatString2    DC.B    '\i,7 + \i,7 = \i,7',0,0
  263.  
  264. ; Data structures - variables
  265.  
  266. vPos        DS        1
  267. count        DS        1
  268. wPointer    DS.L    1
  269. fndWindow    DS.L    1
  270. AppleHandle    DS.L    1
  271. DeskName    DS.L    1
  272. grafporttemp    DS.L    1
  273.  
  274. EventRecord    DS.W    0
  275. What        DS.W    1
  276. Message        DS.L    1
  277. When        DS.L    1
  278. Where        DS.L    1
  279. Modifiers    DS.W    1
  280.